package com.gp.service.impl;


import com.gp.dao.TbBrandDao;
import com.gp.service.CarBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gp.entity.TbBrand;
import com.gp.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service("carBrandService")
public class CarBrandServiceImpl implements CarBrandService {

    @Autowired
    TbBrandDao tbBrandDao;

    @Override
    public CommonResult findAll(Integer page, Integer limit, String name) {
        QueryWrapper<TbBrand> wrapper=new QueryWrapper<>();
        Page<TbBrand> page1=new Page<>(page,limit);
        if (StringUtils.hasText(name)){
            wrapper.like("name",name);
        }
        Page<TbBrand> page2 = tbBrandDao.selectPage(page1, wrapper);
        return new CommonResult(200,"查询成功",page2);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        int i = tbBrandDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

    @Override
    public CommonResult insertBrand(TbBrand tbBrand) {
        int insert = tbBrandDao.insert(tbBrand);
        if (insert > 0) {
            return new CommonResult(200, "添加成功", null);
        } else {
            return new CommonResult(500, "添加失败", null);
        }
    }

    @Override
    public CommonResult updateBrandyId(TbBrand tbBrand) {
        int i = tbBrandDao.updateById(tbBrand);
        if (i> 0) {
            return new CommonResult(200, "修改成功", null);
        } else {
            return new CommonResult(500, "修改失败", null);
        }
    }


}
